{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from sklearn.datasets import make_blobs\n",
    "\n",
    "X,y=make_blobs(n_samples=500,centers=5,random_state=8) #生成样本集，500个点，5类，2个特征\n",
    "plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.cool)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.naive_bayes import BernoulliNB,MultinomialNB,GaussianNB\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)\n",
    "\n",
    "b_nb=BernoulliNB()  #伯努利朴素贝叶斯\n",
    "\n",
    "b_nb.fit(X_train,y_train)\n",
    "\n",
    "score=b_nb.score(X_test,y_test)\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def plot_nb(X,model):\n",
    "    \n",
    "    x_min,x_max=X[:,0].min()-0.5,X[:,0].max()+0.5\n",
    "    y_min,y_max=X[:,1].min()-0.5,X[:,1].max()+0.5\n",
    "    \n",
    "    #画出网格点矩阵\n",
    "    xx,yy=np.meshgrid(np.arange(x_min,x_max,.2),np.arange(y_min,y_max,.2))\n",
    "    \n",
    "    \n",
    "    #用训练好的模型预测网格点矩阵中的每一个点\n",
    "    z=model.predict(np.c_[(xx.ravel(),yy.ravel())]).reshape(xx.shape)\n",
    "\n",
    "    plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1)\n",
    "\n",
    "    plt.scatter(X[:,0],X[:,1],c=y)\n",
    "                    \n",
    "                     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "scaler=MinMaxScaler()  #多项式NB要求输入样本特征值均为非负，因此需要预处理一下\n",
    "scaler.fit(X_train)\n",
    "\n",
    "X_train_scaler=scaler.transform(X_train)\n",
    "X_test_scaler=scaler.transform(X_test)\n",
    "\n",
    "m_nb=MultinomialNB() #多项式朴素贝叶斯\n",
    "\n",
    "m_nb.fit(X_train_scaler,y_train)\n",
    "\n",
    "score=m_nb.score(X_test_scaler,y_test)\n",
    "print(score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_nb(X,m_nb)  #画图多项式NB的分类结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g_nb=GaussianNB()  #高斯朴素贝叶斯\n",
    "g_nb.fit(X_train,y_train)\n",
    "\n",
    "score=g_nb.score(X_test,y_test)\n",
    "\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_nb(X,g_nb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
